package demo;

import com.sun.org.apache.bcel.internal.generic.SWAP;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:DELL
 * Date:2024-10-28
 * Time:19:55
 */
public class CardList {
    //先定义花色数组，花色是常量
    private static final String[] SUIT={"♦","♣","♠","♥"};//被final修饰后大写
    //买牌
    public static List<Card> BuyCards(){
        List<Card> list=new ArrayList<>();
        for (int i = 0; i < SUIT.length; i++) {
            for (int j = 0; j < 13; j++) {
                Card card=new Card(SUIT[i],j);
                list.add(card);
            }
        }
        return list;
    }

    public static void SWAP(List<Card> list,int i,int j) {
        Card tmp=list.get(i);
        list.set(i,list.get(j));
        list.set(j,tmp);
    }
    //洗牌
    public static void shuffle(List<Card> list) {
        Random random=new Random();
        //从后往前生成随机数
        for (int i = list.size()-1; i > 0; i--) {
            int index= random.nextInt(i);
            SWAP(list,index,i);
        }
    }
    public static void main(String[] args) {
        List<Card> list=BuyCards();
        System.out.println(list);
        System.out.println("洗过的牌");
        shuffle(list);
        System.out.println(list);
        //三个人，每人轮流抓5张牌
        //一人5张牌，三个人，所以要用二维数组来写
        List<List<Card>> hand=new ArrayList<>();
        List<Card> hand1=new ArrayList<>();
        List<Card> hand2=new ArrayList<>();
        List<Card> hand3=new ArrayList<>();
        //把他们串起来，加进去
        hand.add(hand1);
        hand.add(hand2);
        hand.add(hand3);
        //控制次数
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                //每次揭牌都是0下标的牌，因为每次揭走就被删除了
                Card card=list.remove(0);
                hand.get(j).add(card);
            }
        }
        //查看三个人揭到的牌
        System.out.println("第一个人的牌："+hand.get(0));
        System.out.println("第一个人的牌："+hand.get(1));
        System.out.println("第一个人的牌："+hand.get(2));
        System.out.println("剩下的牌："+list);
    }
}
